# import serial
from serial import Serial
import sys

print(sys.platform)

try:
    # 使用 with 语句管理串口资源
    with Serial('/dev/ttyUSB0', 9600) as ser:
        if ser.is_open:  # 使用 is_open 属性检查串口是否打开
            print('open')
            ser.write(b'hello world')
            # 使用 in_waiting 属性获取输入缓冲区字节数
            # bytes_in_buffer = ser.in_waiting
            # print(f"Bytes in buffer: {bytes_in_buffer}")
            # data = ser.read(10)
            # print(f"Read data: {data}")
except Exception as e:
    print(f"An error occurred: {e}")